1. /* slftonpr.cpp by K.Tsuru */
  2. // function ID 2010 DRADIX since ver 2.181
  3. #ifndef SN_H
  4. #include "sn.h"
  5. #endif
  6. // for sorting
  7. static int sortBySize(const void *e1, const void *e2) {
  8. return (int)((const SLong *)e2)->DFigures() - (int)((const SLong *)e1)->DFigures();
  9. }
  10. /**************************************************************************
  11. product is evaluated such as
  12. 1) a[0]a[1]...a[n-1] sort in size
  13. 2) a[n-2] <-- a[n-2]a[n-1]
  14. 3) n <-- n-1
  15. repeat n > 1
  16. **************************************************************************/
  17. SLong TournamentProduct(const SNBlock <SLong>& a, const uint N) {
  18. if(N == 1) return a(0);
  19. SLong* r = new SLong[N];
  20. for(uint i = 0; i < N ; i++) r[i] = a(i);
  21. // preliminary tournament --- while(n > 2^p) n--;
  22. uint n = N; // > 1
  23. // 1,000,000! time =0.0
  24. qsort(r, n, sizeof(SLong), sortBySize); // sort into r[0] >= r[1] >= ...
  25. while(n > 1) {
  26. r[n-2] = r[n-2]*r[n-1]; // multiply lowest two
  27. r[n-1].SetZero(); // a loser
  28. n--;
  29. qsort(r, n, sizeof(SLong), sortBySize); // arrange in large order
  30. int ratio = r[0].DFigures()/r[n-1].DFigures();
  31. if(isPow2(n) && (ratio < 4)) break; // 2, 3 or 4 : 12.8sec , 6 : 13.4
  32. }
  33. // t1 = 2.3 sec
  34. // the final selection. n = 2^p here.
  35. while(n > 1) { // selection of both sides
  36. uint j;
  37. for(j = 0; j < n/2; j++) r[j] = r[j]*r[n-j-1];
  38. n /= 2;
  39. }
  40. // the champion is the result(^_^)
  41. SLong R(r[0]);
  42. delete[] r;
  43. // t2 = 10.0 sec (total 12.8 sec)
  44. return R;
  45. }

slftonpr.cpp : last modifiled at 2010/04/23 14:01:46(1,499 bytes)
created at 2017/10/07 10:26:50
The creation time of this html file is 2017/11/09 14:52:03 (Thu Nov 09 14:52:03 2017).